name <- c("A","B","C","D","E","F","G","H","I","J") age <- c(22,43,12,17,29,5,51,56,9,44) sex <- c("M","F","M","M","M","F","F","M","F","F") undertaker <- data.frame(name,age,sex,stringsAsFactors=FALSE) undertaker str(undertaker)# view the properties #data frame name $ variable name undertaker$name <- as.character(undertaker$name) undertaker$sex <- as.character(undertaker$sex) rock <- undertaker rock[1]# only the number refers to column index # right hand side of the comma is for columns # left hand side of the comma is for rows rock[,c(1,3)]## you can specify the columns by index rock[,c("name","age")]## you can specify the columns by name rock[1:5,c(1,3)]## you can specify the rows by index rock[c(1:5),]## you can specify the rows by index head(rock)# top 6 rows with all columns head(rock,7)# top 2 rows with all columns tail(rock,2)# bottom 2 rows attach(rock) rock[age > 20,]# greater than rock[age >= 22,]# greater than equals to rock[age <= 22,]# less than equals to rock[name == "A",c("age","name")]# equals to rock[name == "A",c("age","name")]# equals to rock[name != "D",]# not equals to rock[sex == "F" & age < 50,]# and condition rock[sex == "F" | name == "A",]# or condition ## shortcut of using or function rock[rock$name == "A" | rock$name == "B",] rock[rock$name %in% c("A","B"),]